package distrcomp;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

public class ShipmentIteratorImpl extends UnicastRemoteObject implements ShipmentIterator {
	private static final long serialVersionUID = 1L;
	private Vector<ShippingState> includedStates;
	private Server server; //Reference to server to get the slaves over which we iterate
	private int nextShipment = 0;

	protected ShipmentIteratorImpl(Server server, Vector<ShippingState> states) throws RemoteException {
		super(0, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory(null, null, true));
		this.includedStates = states;
		this.server = server;
	}

	public Shipment next() throws Exception {
		while(this.nextShipment <= this.server.getHighestShipmentID()) {
			Shipment shipment = server.getServer(this.nextShipment).getShipment(this.nextShipment);
			this.nextShipment++;
			if(this.includedStates.contains(shipment.getShippingState())) {
				return shipment;
			}
		}
		return null;
	}

}
